package com.onlineclass.backstagemanagement.service;



import com.onlineclass.backstagemanagement.feign.UserFeign;
import com.onlineclassbackstage.beans.User;
import lombok.AllArgsConstructor;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import javax.annotation.Resource;


public class LoginRealm extends AuthorizingRealm {

    /**
     *
     */
    @Autowired
    UserFeign feign;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        System.out.println(principalCollection.getPrimaryPrincipal().toString());
//        String role = mapper.selectRoleByUser((User) principalCollection.getPrimaryPrincipal());
//        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
//        System.out.println(role);
//        info.addRole(role);
//        return info;
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal = (String) authenticationToken.getPrincipal();
        String username = principal.split(":")[0];
        String rolename = principal.split(":")[1];
        User user = feign.login(username,rolename);
        System.out.println(user);
        SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(user,user.getPwd(),this.getName());
        return info;
    }
}
